//配置axios的全局基本路径
axios.defaults.baseURL='http://localhost:8080/'
// axios.defaults.baseURL='/api' //前端跨域配置
//全局属性配置，在任意组件内可以使用this.$http获取axios对象
//==============axios前置拦截器实现每次携带token======================//
axios.interceptors.request.use(config=>{
    let token = localStorage.getItem("token");
    if(token){
        //携带token
        config.headers["token"] = token;
    }

    return config;
},error => {
    Promise.reject(error)
})
//==============axios前置拦截器实现每次携带token======================//
//==============axios后台拦截器实现后端已经退出登录的跳转登录页面======================//
axios.interceptors.response.use(config=>{
    let data = config.data;
    if(!data.success && "noLogin"===data.message)
    {
        localStorage.removeItem("token");
        localStorage.removeItem("loginInfo");
        location.href = "/login.html";
    }
    return config;
},error => {
    Promise.reject(error)
})
//==============axios后台拦截器实现后端已经退出登录的跳转登录页面======================//
Vue.prototype.$http = axios


//前端的登录拦截-common.js会被所有的界面导入
// 判断localStorage中是否有logininfo
let url = location.href;
//不是登录且不是注册，就要拦截
if(url.indexOf("login.html")==-1 && url.indexOf("register.html")==-1 && url.indexOf("callback.html")==-1 && url.indexOf("binder.html")==-1)
{
    let loginInfo = localStorage.getItem("loginInfo");
    if(!loginInfo){
        location.href = "/login.html"
    }
}


//公共方法-解析url参数为参数对象
function parseUrlParams2Obj(url) {
    // //http://bugtracker.itsource.cn/callback.html?code=011sS0000gRmGK1LaU300gLhr81sS00v&state=1
    let paramsStr = url.substring(url.indexOf("?")+1) //code=011sS0000gRmGK1LaU300gLhr81sS00v&state=1
    let paramsStrs = paramsStr.split("&"); //[code=011sS0000gRmGK1LaU300gLhr81sS00v,state=1]
    let paramObj = {};
    for(let i=0;i<paramsStrs.length;i++){
        let paramStrTmp = paramsStrs[i];//code=011sS0000gRmGK1LaU300gLhr81sS00v
        let paramName = paramStrTmp.split("=")[0];
        let paramVal = paramStrTmp.split("=")[1];
        paramObj[paramName] = paramVal;
    }
    return paramObj;
}
